package com.ruibang.glass.quality.component.subscriber;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.quality.BcStationHqDetail;
import com.ruibang.glass.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-05-09  14:27
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Component
public class BcStationHqDetailMsgReceiver {


    @Autowired
    @Qualifier("myRedisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * @Description: bc_station_hq_detail消息处理
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-09 14:27
     */
    public void receiveMessage(String message, String channel) {
        if (StringUtils.isNotBlank(message)) {
            BcStationHqDetail bcStationHqDetail = JSON.parseObject(message, BcStationHqDetail.class);
//            log.error("bcStationHqDetail ---实体---: " + message);
            if (bcStationHqDetail != null && StringUtils.isNotBlank(bcStationHqDetail.getId()) && bcStationHqDetail.getStationStartTime() != null) {
                LocalDateTime localDateTime = DateUtil.toLocalDateTime(Instant.ofEpochMilli(Long.parseLong(bcStationHqDetail.getStationStartTime())));
                if (localDateTime != null) {
                    redisTemplate.opsForHash().put(RedisKey.HOT_ID_MAPPING_STATION_START_TIME_KEY, bcStationHqDetail.getId(), DateUtils.localDateTimeToString(localDateTime, Constants.DATE_TIME_FORMATTER_STR));
                }
            }
        }
    }
}
